﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.UI;
using GTS.Channel.Api.Entity;
using Pub.Class;
using GTS.Channel.Api.Helper;

namespace GTS.Channel.Api {
    /// <summary>
    /// 通联天地SMS和IVR的MO MT MR接收数据接口
    /// </summary>
    public class TongLian : ISMSIVR {
        /// <summary>
        /// SPCode
        /// </summary>
        public string SPCode = "TongLian";
        /// <summary>
        /// SMS MO的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMOData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.IsHidden = false;
            info.Price = 10;
            info.DeviceCode = "GENERICDEVICE";

            //接收值
            string result = Request2.GetRequestInputStream().UrlDecode();
            info.Mobile = result.GetMatchingValues("<mobile>(.+?)</mobile>", "<mobile>", "</mobile>").FirstOrDefault() ?? "";
            info.LongPhone = result.GetMatchingValues("<spcode>(.+?)</spcode>", "<spcode>", "</spcode>").FirstOrDefault() ?? "";
            info.LinkID = (result.GetMatchingValues("<linkid>(.+?)</linkid>", "<linkid>", "</linkid>").FirstOrDefault() ?? "");
            info.ReceiveQuery = result.GetMatchingValues("<momsg>(.+?)</momsg>", "<momsg>", "</momsg>").FirstOrDefault() ?? "";;

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.ReceiveQuery.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;

            string[] infos = info.ReceiveQuery.Split('_');
            if (infos.Length >= 4) {
                info.MountCode = infos[1].ToInt();
                info.GameCode = infos[2].PadLeft(5, '0');
                info.ChargeCode = infos[3];
            } else {
                info.MountCode = 493;
                info.GameCode = "00999";
                info.ChargeCode = "9";
            }

            //如果数据库中存在价格以数据库里为主
            ChannelInfo extInfo = Base.GetChannelInfo(SPCode, info.LongPhone, info.ReceiveQuery);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }

            return info;
        }
        /// <summary>
        /// SMS MT的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMTData() {
            return null;
        }
        /// <summary>
        /// SMS MR的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMRData() {
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;

            //接收值
            string result = Request2.GetRequestInputStream().UrlDecode();
            info.LinkID = (result.GetMatchingValues("<msgid>(.+?)</msgid>", "<msgid>", "</msgid>").FirstOrDefault() ?? "");
            info.ResponseStatus = (result.GetMatchingValues("<reportstat>(.+?)</reportstat>", "<reportstat>", "</reportstat>").FirstOrDefault() ?? "") == "DELIVRD" ? 1 : 0;
            info.Price = ((result.GetMatchingValues("<feecode>(.+?)</feecode>", "<feecode>", "</feecode>").FirstOrDefault() ?? "0").ToInt(10) / 10).IfLessThan(10, 10);
            info.MRDate = DateTime.Now;

            if (info.LinkID.IsNullEmpty()) return null;

            return info;
        }
        /// <summary>
        /// IVR的参数数据
        /// </summary>
        /// <returns></returns>
        public IVRDealData GetIVRMRData() {
            return null;
        }
        /// <summary>
        /// 输出消息
        /// </summary>
        /// <param name="res"></param>
        public void Output(ResultEnum res) {
            Msg.WriteEnd(res.ToString());
        }
    }
}

